package com.cq.oa.web.sm.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.session.SessionManger;
import oa.base.util.Page;
import oa.base.web.Action.BaseAction;

import com.cq.oa.domain.sm.Res;
import com.cq.oa.domain.sm.RoleRes;
import com.cq.oa.service.sm.ResService;
import com.cq.oa.service.sm.RoleService;
import com.cq.oa.utils.TreeNodeUtils;

public class SmResAction extends BaseAction<Res>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2283944864146115800L;
	
	private ResService resService;
	
	
	private RoleService roleService;
	
	private Long roleId;
	
	

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setResService(ResService resService) {
		this.resService = resService;
	}

	@Override
	protected Class<Res> getEntityClass() {
		return Res.class;
	}
	
	public String findByPage() throws Exception {
		Page<Res> users = resService.findByPage(
				new HashMap<String, Object>(), new HashMap<String, Object>(),
				new HashMap<String, Object>(), super.getPageNo(),
				super.getPageSize());
		this.renderJsonSuccess(jsonUtils.getExtJson(users, new String[] {}));
		return JSON;
	}
	
	public String operate(){
		if(null!=entity.getSid()){
			entity = resService.findByParmkeys(entity.getSid());
		}
		return "operate";
	}
	
	public String save(){
		if(null!=entity.getSid()){
			this.resService.update(entity);
		}else{
			this.resService.create(entity);
		}
		this.renderJsonSuccess();
		return JSON;
	}
	public String delete(){
		this.resService.delete(entity.getSid());
		this.renderJsonSuccess();
		return JSON;
	}
	/**
	 * 菜单树
	 * @return
	 * lilang 
	 * 2013-9-11下午4:23:30
	 * @throws Exception 
	 */
	public String findChildTree() throws Exception{
		if(id==null){
			id="0";
		}
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("pid", Long.valueOf(id));
		params.put("orgSid", SessionManger.getOrgId());
		List<Res> res=resService.findParByList(params);
		this.renderJsonSuccess(jsonUtils.getExtArrayJson(TreeNodeUtils.bulidRes2TreeNode(res)));
		return JSON;
	}
	
	/**
	 * 菜单树
	 * @return
	 * lilang 
	 * 2013-9-11下午4:23:30
	 * @throws Exception 
	 */
	public String findAllChildTree() throws Exception{
		List<RoleRes> ress = this.roleService.findRoleResById(roleId);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("orgSid", SessionManger.getOrgId());
		List<Res> res=resService.findParByList(params);
		this.renderJsonSuccess(jsonUtils.getExtArrayJson(TreeNodeUtils.bulidRes2TreeNode(res,0L,bulidRoleRes(ress))));
		return JSON;
	}
	
	public Map<Long,Long> bulidRoleRes(List<RoleRes> ress){
		Map<Long,Long> maps = new HashMap<Long, Long>();
		for(RoleRes res:ress){
			maps.put(res.getResSid(), res.getResSid());
		}
		return maps;
	}

}
